//
//  KSAlertController.swift
//  ANOA
//
//  Created by 邱仙凯 on 2023/2/11.
//  Copyright © 2023 zwb. All rights reserved.
//  弹窗 提醒  提示

import UIKit
class ComAlertV:UIView {
   
    private var _alertCover = UIView()
 
   func present() {
       let cover = UIView()
       cover.frame = UIScreen.main.bounds
        cover.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.35)
        cover.addSubview(self)
  
       self.snp.makeConstraints { make in
           make.center.equalToSuperview()
           make.width.equalTo(KSGlobal.screenW * 0.85)
       }
     
       cover.alpha = 0
       self.alpha = 0.5
       if let kwindow =  UIApplication.shared.windows.filter({$0.isKeyWindow}).first {
           kwindow.rootViewController?.view.addSubview(cover)
           cover.layoutIfNeeded()
           self.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
           
           UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseInOut) {
               cover.alpha = 1
               self.alpha = 1
               self.transform = CGAffineTransform.identity
           }
           _alertCover = cover
        }
 
   }
    // 正在显示
    func isShow() -> Bool {
        return _alertCover.superview.isSome
    }

   
   func dismiss() {
       UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseInOut) {
           self._alertCover.alpha = 0
       } completion: { isCompleted in
           if isCompleted {
               self.removeFromSuperview()
               self._alertCover.removeFromSuperview()
               KSLog("销毁了")
           }
           KSLog("外面来了")
       }
   }
     var fieldArr = [(String,UITextView)]()
    
    lazy var titleLab:UILabel = {
       let v = UILabel()
        v.numberOfLines = 0
        v.textAlignment = .center
        v.font = UIFont.systemFont(ofSize: 23)
        return v
    }()
    lazy var contentLab:UILabel = {
       let v = UILabel()
        v.numberOfLines = 0
        v.textAlignment = .center
        v.font = UIFont.systemFont(ofSize: 17)
        return v
    }()
 
    var lastBtn:UIButton?
    var lastV:UIView?
    // textFields 分两部分，v之前为显示字段名称，v之后为默认值，没有v则都是显示名称，没有默认值
    func add(title:String,message:String,textFields:[String] = [],operateS:[nameBtnAction]) {
        titleLab.text = title
        contentLab.text = message
        self.lastV = contentLab
        for i in  0..<textFields.count {
            
            let bg = UIView()
            self.addSubview(bg)
            bg.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                if i == 0 {
                    make.top.equalTo(contentLab.snp.bottom).offset(20)
                } else {
                    make.top.equalTo(self.lastV!.snp.bottom).offset(15)
                }
               make.width.equalToSuperview().multipliedBy(0.9)
                make.height.equalTo(50)
             }
            
            var fieldName = ""
            var fieldValue = ""
            if textFields[i].contains("v") {
                fieldName =  textFields[i].preChar(char: "v").unwrap
                fieldValue =  textFields[i].sufChar(char: "v").unwrap
            } else {
                fieldName = textFields[i]
            }
           
           
            
            
            
            let v = UILabel()
            v.numberOfLines = 0
            bg.addSubview(v)
             v.text = fieldName
            v.textAlignment = .left
              v.font = UIFont.systemFont(ofSize: 16)
             v.snp.makeConstraints { make in
                 make.left.top.bottom.equalToSuperview()
                 make.width.equalToSuperview().multipliedBy(0.25)
             }
            let field = UITextView()
            field.text = fieldValue
            bg.addSubview(field)
            field.font = UIFont.systemFont(ofSize: 16)
            fieldArr.append((textFields[i],field))
            field.layer.borderWidth = 1
            field.layer.borderColor = UIColor(kshex: "eeeeee").cgColor
            field.layer.cornerRadius = 8
             field.snp.makeConstraints { make in
                make.right.top.bottom.equalToSuperview()
                make.width.equalToSuperview().multipliedBy(0.75)
             }
             self.lastV = bg
        }
        for i in  0..<operateS.count {
            let one = operateS[i]
            let v = UIButton()
            self.addSubview(v)
             v.setTitle(one.name, for: .normal)
             v.setTitleColor(UIColor(kshex: "2222ff"), for: .normal)
            v.layer.borderWidth = 1
            v.layer.borderColor = UIColor(kshex: "2222ff").cgColor
            v.layer.cornerRadius = 8
             v.titleLabel?.font = UIFont.systemFont(ofSize: 20)
            v.addTouchUpInSideAction(one.action)
            v.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                if i == 0 {
                    make.top.equalTo(self.lastV!.snp.bottom).offset(50)
                } else {
                    make.top.equalTo(self.lastBtn!.snp.bottom).offset(15)
                }
                if i == operateS.count - 1 {
                    make.bottom.equalToSuperview().offset(-30)
                }
                make.width.equalTo(200)
                make.height.equalTo(35)
             }
            self.lastBtn = v
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
         self.layer.cornerRadius = 5
         self.addSubview(titleLab)
        self.addSubview(contentLab)
         titleLab.snp.makeConstraints { make in
             make.top.equalToSuperview().offset(30)
             make.centerX.equalToSuperview()
              make.width.equalToSuperview().multipliedBy(0.9)
             make.bottom.equalTo(contentLab.snp.top).offset(-20)
         }
        
         contentLab.snp.makeConstraints { make in
             make.centerX.equalToSuperview()
             make.width.equalToSuperview().multipliedBy(0.9)
           }
   
      }
     required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

    
    
}
 
